package cn.hml.basic.controller;

import cn.hml.basic.dto.LoginDto;
import cn.hml.basic.exception.BusinessException;
import cn.hml.basic.util.AjaxResult;
import cn.hml.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            //return logininfoService.accountLogin(loginDto);
            Map<String,Object> map = logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }


    /**
     * 微信登录接口
     * @param code
     * @return
     */

    @GetMapping ("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code")String code){
        try {
           return logininfoService.wechatLogin(code);
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }
    /**
     * 微信绑定接口
     * @param map
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {
            return logininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }
}
